package com.renge.vodservice.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.renge.exception.GuliException;
import com.renge.vodservice.service.VodService;
import com.renge.vodservice.utils.AliyunVodSDKUtils;
import com.renge.vodservice.utils.ConstantPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
@Service
public class VodServiceImpl implements VodService {

    //上传视频到阿里云
    @Override
    public String uploadVod(MultipartFile file) {
        try {
            //封装属性
            String accessKeyId= ConstantPropertiesUtil.KEYID;
            String accessKeySecret=ConstantPropertiesUtil.KEYSECRET;
            String title=file.getOriginalFilename().subSequence(0, file.getOriginalFilename().lastIndexOf(".")).toString();
            String fileName=file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
            if (response.isSuccess()) {
                String videoId = response.getVideoId();
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                return videoId;
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
                return null;
            }
        }catch (Exception e){
            GuliException guliException = new GuliException();
            guliException.setCode(250);
            guliException.setMsg("上传出现问题！！");
            throw guliException;
        }

    }
     //根据视频id从阿里云删除视频
    @Override
    public void deleteVideoById(String videoId) {
        //封装属性
        String accessKeyId= ConstantPropertiesUtil.KEYID;
        String accessKeySecret=ConstantPropertiesUtil.KEYSECRET;
        try {
            //初始化client对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);
            //向requset设置视频id
            final DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            //调用初始化对象client的方法实现删除
            client.getAcsResponse(request);
        } catch (ClientException e) {
           throw new GuliException(20001,"不存在此视频或者造成错误");
        }
    }
        //根据视频id获得播放凭证
    public String getPlayAuthById(String videoId){
        //封装属性
        String accessKeyId= ConstantPropertiesUtil.KEYID;
        String accessKeySecret=ConstantPropertiesUtil.KEYSECRET;
        try {
            //初始化client对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);
            //向requset设置视频id
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            //调用初始化对象client的方法返回获得凭证的Respons
            GetVideoPlayAuthResponse acsResponse = client.getAcsResponse(request);
            //获得凭证并返回
            return acsResponse.getPlayAuth();
        } catch (ClientException e) {
            throw new GuliException(20001,"不存在此视频或者造成错误");
        }
    }

}
